package cn.dsttl3.tupan.utils.aria2;

import cn.dsttl3.tupan.bean.aria2.Aria2Bean;
import cn.dsttl3.tupan.bean.aria2.Aria2Header;
import cn.dsttl3.tupan.utils.DataUtils;
import com.google.gson.Gson;
import ohos.app.Context;
import ohos.data.preferences.Preferences;
import okhttp3.*;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * 使用Aria2下载
 */
public class Aria2 {


    public String download(List<String> durls, Context context){
        try {
            String aria2URL = new Aria2().getAria2URL(context);
            OkHttpClient okHttpClient = (new OkHttpClient.Builder()).build();
            Aria2Bean aria2Bean = new Aria2Bean();
            aria2Bean.setId(UUID.randomUUID().toString());
            aria2Bean.setJsonrpc("2.0");
            aria2Bean.setMethod("aria2.addUri");
            List<Object> dls = new ArrayList<>();
            dls.add(durls);
            Aria2Header header = new Aria2Header();
            List<String> list = new ArrayList<>();
            list.add("User-Agent:pan.baidu.com");
            header.setHeader(list);
            dls.add(header);
            aria2Bean.setParams(dls);
            Gson gson = new Gson();
            String json = gson.toJson(aria2Bean);
            System.out.println(json);
            RequestBody requestBody = FormBody.create(json,MediaType.parse("application/json; charset=utf-8"));

            Request request = (new Request.Builder()).url(aria2URL).post(requestBody).build();
            Response response = okHttpClient.newCall(request).execute();
            return "已开始下载...";
        } catch (IOException e) {
            return "下载请求出错：" + e.getMessage();
        }
    }

    /**
     *
     * @return 测试连接
     */
    public boolean test(Context context){
        try {
            String aria2URL = new Aria2().getAria2URL(context);
            OkHttpClient okHttpClient = (new OkHttpClient.Builder()).build();
            Aria2Bean aria2Bean = new Aria2Bean();
            aria2Bean.setId(UUID.randomUUID().toString());
            aria2Bean.setJsonrpc("2.0");
            aria2Bean.setMethod("aria2.tellActive");
            Gson gson = new Gson();
            String json = gson.toJson(aria2Bean);
            RequestBody requestBody = FormBody.create(json,MediaType.parse("application/json; charset=utf-8"));
            Request request = (new Request.Builder()).url(aria2URL).post(requestBody).build();
            okHttpClient.newCall(request).execute();
            return true;
        } catch (IOException e) {
            System.out.println("Aria2连接出错：" + e.getMessage());
            return false;
        }
    }

    public boolean test(String ip, String port){
        try {
            String aria2URL = "http://" + ip +":" + port + "/jsonrpc";
            OkHttpClient okHttpClient = (new OkHttpClient.Builder()).build();
            Aria2Bean aria2Bean = new Aria2Bean();
            aria2Bean.setId(UUID.randomUUID().toString());
            aria2Bean.setJsonrpc("2.0");
            aria2Bean.setMethod("aria2.tellActive");
            Gson gson = new Gson();
            String json = gson.toJson(aria2Bean);
            RequestBody requestBody = FormBody.create(json,MediaType.parse("application/json; charset=utf-8"));
            Request request = (new Request.Builder()).url(aria2URL).post(requestBody).build();
            okHttpClient.newCall(request).execute();
            return true;
        } catch (IOException e) {
            System.out.println("Aria2连接出错：" + e.getMessage());
            return false;
        }
    }

    private String getAria2URL(Context context){
        Preferences preferences = new DataUtils().getPreferences(context);
        String ip = preferences.getString("aria2_ip","192.168.3.1");
        String port = preferences.getString("aria2_port","6800");
        return  "http://" + ip +":" + port + "/jsonrpc";
    }
}
